package com.ch999.detect.utils;

import android.content.Context;
import android.media.MediaRecorder;
import android.util.Log;

import com.scorpio.mylib.utils.FileUtil;

import java.io.File;
import java.io.IOException;


/**
 * Created by zhulang on 2017/12/29 0017.
 * Email by arlang@9ji.com
 * <p>
 * 录音音量控件
 */

public class RecordButton  {

    String TAG = this.getClass().getSimpleName();
    private OnFinishedRecordListener finishedListener;

    private MediaRecorder mRecorder;
    private ObtainDecibelThread mthread;
    private String mFilePath;

    private int count = 0 ;

    /**
     * 录音完成的回调
     *
     * @param listener
     */
    public void setOnFinishedRecordListener(OnFinishedRecordListener listener) {
        finishedListener = listener;
    }

    public void setDefaultFilePath(Context context) {
        File dir = new File(FileUtil.appDataPath(context) , "sounds");
        if (!dir.exists()) {
            dir.mkdirs();
        }

        File soundFile = new File(dir, System.currentTimeMillis() + ".amr");
        mFilePath = soundFile.getAbsolutePath();

        if (!soundFile.exists()) {
            try {
                soundFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
    public void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile(mFilePath);
        try {
            mRecorder.prepare();
            mRecorder.start();
            mthread = new ObtainDecibelThread();
            mthread.start();
        } catch (Exception e) {
            e.printStackTrace();

            if (finishedListener != null) {
                finishedListener.onRecordFail();
                stopRecording();
            }
        }

       // finishedListener.onStratRecord();

    }

    private void stopRecording() {

        if (mthread != null) {
            mthread.exit();
            mthread = null;
        }

        //捕获异常

        if (mRecorder != null) {
            try {
                mRecorder.stop();
            } catch (Exception j) {

                if (finishedListener != null)
                    finishedListener.onRecordFail();

            }
            mRecorder.reset();
            mRecorder.release();

            mRecorder = null;
        }

        count =0;

    }

    private class ObtainDecibelThread extends Thread {

        private volatile boolean running = true;

        public void exit() {
            running = false;
        }

        @Override
        public void run() {
            while (running) {
               try {
                    Thread.sleep(200);
                }  catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (mRecorder == null || !running) {
                    break;
                }


                if (count > 10) {
                    if (finishedListener != null) {
                        finishedListener.onRecordFail();
                        stopRecording();
                    }
                } else {
                   count ++;
                }

                if (mRecorder != null) {
                    int x = mRecorder.getMaxAmplitude();
                    Log.e(TAG, "run: " + x);
                    if (x > 500) {
                        if (finishedListener != null) {
                            finishedListener.onStartRecordSuccess();
                            stopRecording();
                        }
                    }
                }


            }
        }

    }


    /**
     * 完成录音回调
     */
    public interface OnFinishedRecordListener {

      //  void onStratRecord();
       // void onFinishedRecord(String audioPath, int intervalTime);

        void onRecordFail();

        void onStartRecordSuccess();
    }
}
